let request = require("./request")

//流程定义实例flowable的rest调用
let handler = {
    start_a_process: async function (params,login_info) {
        let data={
            "processDefinitionId":params.f_process_def_id,
            "returnVariables":true,
            "name":params.name,
            "variables": [
               {
                 "name":"starter",
                 "value":login_info.emp_id
               },
               ...params.variables
            ]
         }
        let result = await request.post('runtime/process-instances',data);
        if (result.code != 201) {
            throw({
                name: "发起流程错误",
                message: result.code+" "+result.data.message+" "+result.data.exception
            });
        }
        else {
            return result.data;
        }
    },
    query_tasks: async function (params) {
        let data={
            "processInstanceId":params.f_process_id,
            "includeProcessVariables": true
        }
        let result = await request.post('query/tasks',data);
        if (result.code != 200) {
            throw({
                name: "查询任务错误",
                message: result.code+" "+result.data.message+" "+result.data.exception
            });
        }
        else {
            return result.data;
        }
    },
    update_variables: async function (params) {
        let f_process_id=params.f_process_id;
        let data=params.variables;
        let result = await request.put('runtime/process-instances/'+f_process_id+'/variables',data);
        if (result.code != 201) {
            throw({
                name: "更新流程变量错误",
                message: result.code+" "+result.data.message+" "+result.data.exception
            });
        }
        else {
            return result.data;
        }
    }
}

module.exports = handler;